package cn.edu.bzu.ie.sf.mealsmisweb.controller;

import cn.edu.bzu.ie.sf.mealsmisweb.common.MyUtils;
import cn.edu.bzu.ie.sf.mealsmisweb.entity.Department;
import cn.edu.bzu.ie.sf.mealsmisweb.entity.District;
import cn.edu.bzu.ie.sf.mealsmisweb.mapper.DepartmentMapper;
import cn.edu.bzu.ie.sf.mealsmisweb.service.DepartmentService;
import cn.edu.bzu.ie.sf.mealsmisweb.service.DistrictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version v1.0.0
 * @Package : cn.edu.bzu.ie.sf.mealsmisweb.controller
 * @Description : 部门信息控制器类
 * @Create on : 2021/8/10 10:44
 * @author: Feng Song 宋锋
 **/
@RestController
@RequestMapping(value = "/depart")
public class DepartmentController {
    @Autowired
    private DistrictService districtService;    // 区县业务实例
    @Autowired
    private DepartmentService departmentService;  // 部门业务实例

    /**
     * 获取所有的部门信息列表
     * @return List<Department> 实例
     */
    @RequestMapping(value="/getAllDepartmentList")
    public List<Department> getAllDepartmentList(){
        return departmentService.getAllDepartmentList();
    }

    /**
     * 获取某个部门下的所有部门信息列表
     * @param pid 父部门id
     * @return List<Department> 实例
     */
    @RequestMapping(value="/getAllDepartmentListByPid")
    public List<Department> getAllDepartmentListByPid(int pid){
        return departmentService.getAllDepartmentListByPid(pid);
    }

    /**
     * 获取所有的父级部门，deptype=0或1，用于部门管理中部门添加、修改
     * @return List<Department> 实例
     */
    @RequestMapping(value="/getAllParentDepartmentList")
    public List<Department> getAllParentDepartmentList(){
        return departmentService.getAllParentDepartmentList();
    }

    /**
     * 获取某个部门信息
     * @param id 部门的id
     * @return Department 实例
     */
    @RequestMapping(value="/getDepartmentById")
    public Department getDepartmentById(int id){
        return departmentService.getDepartmentById(id);
    }

    /**
     * 获取区县列表、支队与大队列表map的方法
     * @return Map 含有区县、支队与大队列表（一次访问，获取两种数据，减少手机与服务端的交互次数）
     */
    @RequestMapping(value="/getDistAndDepListData")
    public Map<String, Object> getDistAndDepListData(){
        Map<String, Object> map = new HashMap<>();
        List<District> districtList = districtService.getAllDistrictList();
        map.put("distList", districtList);
        List<Department> departmentList = departmentService.getAllParentDepartmentList();
        map.put("depList", departmentList);

        return map;
    }

    /**
     * 添加部门信息的方法
     * @param item 要添加的部门信息
     * @return int 影响的行数
     */
    @PostMapping(value="/addDepartment")
    public int addDepartment(Department item){
        item.setCreate_dt(MyUtils.getDateTimeStrFromNow());
        if(item.getDistid()==0){
            item.setDistid(null);
        }
        return departmentService.addDepartment(item);
    }

    /**
     * 修改部门信息的方法
     * @param item 要修改的部门信息
     * @return int 影响的行数
     */
    @PostMapping(value = "/editDepartment")
    public int editDepartment(Department item){
        return departmentService.editDepartment(item);
    }

    /**
     * 删除部门信息的方法
     * @param id 要删除的部门的id
     * @return int 影响的行数
     */
    @PostMapping(value="/delDepartment")
    public int delDepartment(int id){
        return departmentService.delDepartment(id);
    }
}
